﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;

using CMN.Models;
using CMN.Models.Interfaces;
using CMN.Entity;
using System.Web.Routing;
using CMN.Common;
using Newtonsoft.Json;
using Newtonsoft.Json.Converters;
using CMN.Controllers.Filters;
namespace CMN.Controllers
{
    [ExceptionFilter]
    [LoggerFilter]
    [UserAuthorize]
    public class Normal_ZTMapController : Controller
    {
        
        // 返回json字符串
        protected string returnJson = string.Empty;

        /// <summary>
        /// 转到View页面
        /// </summary>
        /// <returns></returns>
        public ViewResult Index()
        {
            return View("Index");//页面地址
        }

        /// <summary>
        /// 返回专题详细分类列表
        /// </summary>
        /// <returns>json</returns>
        public string List(string actionId)
        {
            //返回json数组
          
            int pageSize = (int)Utils.GetQueryAndFormString("limit", this, 4, 1, DataType.Int);
            int pageIndex = (int)Utils.GetQueryAndFormString("start", this, 9, 1, DataType.Int);
            QueryParam qp = new QueryParam();
            qp.PageIndex = pageIndex / pageSize + 1; ;
            qp.PageSize = pageSize;
            int recordNum = 0;
            if (!string.IsNullOrEmpty(actionId))
                qp.Where = string.Concat(" pId=", actionId, " ");
            IsoDateTimeConverter jsDataConvert= new Newtonsoft.Json.Converters.IsoDateTimeConverter();
            jsDataConvert.DateTimeFormat = "yyyy-MM-dd";
            IDataService<Normal_ZTMapEntity> Normal_ZTMapSv = ServiceBuilder.BuildNormal_ZTMapService();
            List<Normal_ZTMapEntity> normal_ztmapList = Normal_ZTMapSv.List(qp, out recordNum);
            returnJson = string.Concat("{totalPorperty:", recordNum, ",root:", JsonConvert.SerializeObject(normal_ztmapList, Formatting.Indented, jsDataConvert), "}");
            return returnJson;
 
        }

        /// <summary>
        /// 查看专题详细分类 
        /// </summary>
        /// <param name="id"></param>
        /// <returns>json</returns>
        public string Look2(string actionId)
        {
            IDataService<Normal_ZTMapEntity> Normal_ZTMapSv = ServiceBuilder.BuildNormal_ZTMapService();
            int intId = Utils.StringToInt(actionId, 0);
            Normal_ZTMapEntity normal_ztmap = Normal_ZTMapSv.Look(intId);
            IDataService<Module_ArticleEntity> articleSv = ServiceBuilder.BuildModule_ArticleService();

            QueryParam qp = new QueryParam();
            qp.PageIndex = 1;
            qp.PageSize = 100;

            int recordNum = 0;
             List<Module_ArticleEntity> articleList=new List<Module_ArticleEntity>();
             if (!string.IsNullOrEmpty(normal_ztmap.IdList))
             {
                 qp.Where = string.Concat(" id in (", normal_ztmap.IdList, ") ");
                 articleList = articleSv.List(qp, out recordNum);
             }

                 returnJson = string.Concat("{totalPorperty:", recordNum, ",root:", JsonConvert.SerializeObject(articleList), "}");

            return returnJson;
        }

        /// <summary>
        /// 查看专题详细分类 
        /// </summary>
        /// <param name="id"></param>
        /// <returns>json</returns>
        public string Look(string actionId)
        {
           IDataService<Normal_ZTMapEntity> Normal_ZTMapSv = ServiceBuilder.BuildNormal_ZTMapService();
            int intId=Utils.StringToInt(actionId,0);
            Normal_ZTMapEntity normal_ztmap = Normal_ZTMapSv.Look(intId);
            if (normal_ztmap != null)
                returnJson = string.Concat("{success:true,data:", JsonConvert.SerializeObject(normal_ztmap), "}");
            else
                returnJson = "{success:'false'}";
            return returnJson;
        }

        /// <summary>
        /// 更新专题详细分类
        /// </summary>
        /// <param name="actionId"></param>
        /// <returns>json</returns>
        public string Update(string actionId)
        {
            int intId = Utils.StringToInt(actionId, 0);
            if (intId != 0)
            {
                Normal_ZTMapEntity normal_ztmap = new Normal_ZTMapEntity();
                normal_ztmap.DataTable_Action = DataTableAction.Update;
                normal_ztmap.Id = intId;
                normal_ztmap.PId = (Int32)Utils.GetQueryAndFormString("PId", this, 10,  1 , DataType.Int);
                normal_ztmap.Title = (String)Utils.GetQueryAndFormString("Title", this, 60,  1 , DataType.Str);
                normal_ztmap.Summary = (String)Utils.GetQueryAndFormString("Summary", this, 600,  0, DataType.Str);
                normal_ztmap.TopImg = (String)Utils.GetQueryAndFormString("TopImg", this, 300,  0, DataType.Str);
                normal_ztmap.OrderNum = (Int32)Utils.GetQueryAndFormString("OrderNum", this, 10,  0, DataType.Int);
                normal_ztmap.Enabled = (Int32)Utils.GetQueryAndFormString("Enabled", this, 3, 1, DataType.Int);
                normal_ztmap.IdList = (String)Utils.GetQueryAndFormString("IdList", this, 2000, 0, DataType.Str);
                IDataService<Normal_ZTMapEntity> Normal_ZTMapSv = ServiceBuilder.BuildNormal_ZTMapService();
                if (Normal_ZTMapSv.Update(normal_ztmap)== "")
                {
                    returnJson = "{success:'true'}";
                }
                else
                    returnJson = "{success:'false'}";
            }
            else
            {
                returnJson = "{success:'false'}";
            }
           
            return returnJson;
        }



        /// <summary>
        /// 新增专题详细分类
        /// </summary>
        /// <returns>json</returns>
        public string Insert(string actionId)
        {
            Normal_ZTMapEntity normal_ztmap = new Normal_ZTMapEntity();
            normal_ztmap.DataTable_Action = DataTableAction.Insert;
            normal_ztmap.PId = (Int32)Utils.GetQueryAndFormString("PId", this, 10,  1 , DataType.Int);
            normal_ztmap.Title = (String)Utils.GetQueryAndFormString("Title", this, 60,  1 , DataType.Str);
            normal_ztmap.Summary = (String)Utils.GetQueryAndFormString("Summary", this, 600,  0, DataType.Str);
            normal_ztmap.TopImg = (String)Utils.GetQueryAndFormString("TopImg", this, 300,  0, DataType.Str);
            normal_ztmap.OrderNum = (Int32)Utils.GetQueryAndFormString("OrderNum", this, 10,  0, DataType.Int);
            normal_ztmap.Enabled = (Int32)Utils.GetQueryAndFormString("Enabled", this, 3, 1, DataType.Int);
            normal_ztmap.IdList = (String)Utils.GetQueryAndFormString("IdList", this, 2000,  0, DataType.Str);
           IDataService<Normal_ZTMapEntity> Normal_ZTMapSv = ServiceBuilder.BuildNormal_ZTMapService();
            if (Normal_ZTMapSv.Insert(normal_ztmap)== "")
            {
                returnJson = "{success:'true'}";
            }
            else
                returnJson = "{success:'false'}";
            return returnJson;
        }

        


        /// <summary>
        /// 新增标签列表
        /// </summary>
        /// <returns>json</returns>
        public string Insert2(string actionId)
        {
            string articleIdList = (string)Utils.GetQueryAndFormString("moduleIdList", MethodType.Get, 300, 0, DataType.Str);
            Common.RegExpression.NumAndBarRegExpression regex = new CMN.Common.RegExpression.NumAndBarRegExpression();
            if (regex.IsMatch(articleIdList))
            {
               
                IDataService<Normal_ZTMapEntity> Normal_ZTMapSv = ServiceBuilder.BuildNormal_ZTMapService();
                int intId = Utils.StringToInt(actionId, 0);
                Normal_ZTMapEntity normal_ztmap = Normal_ZTMapSv.Look(intId);
                IDataService<Module_ArticleEntity> articleSv = ServiceBuilder.BuildModule_ArticleService();
                string[] idList = articleIdList.Split('_');
              
                string[] articleTagList = null;
                articleTagList = normal_ztmap.IdList.Split(',');
                for (int i = 0; i < idList.Length; i++)
                {
                    int articleId = Utils.StringToInt(idList[i], 0);

                    bool isHas = false;
                    //是否已经包括
                    for (int j = 0; j < articleTagList.Length; j++)
                    {
                        if (articleTagList[j] == idList[i])
                        {
                            isHas = true;
                            break;
                        }

                    }
                    if (!isHas)
                    {
                        if (string.IsNullOrEmpty(normal_ztmap.IdList))
                        {
                            normal_ztmap.IdList = idList[i];
                        }
                        else
                            normal_ztmap.IdList = string.Concat(normal_ztmap.IdList, ",", idList[i]);
                        
                    }
                }
                Normal_ZTMapSv.Update(normal_ztmap);
                returnJson = "{success:'true'}";


            }
            else
            {
                returnJson = "{success:'false',message:'参数类型不正确'}";
            }
            return returnJson;

        }

        /// <summary>
        /// 删除专题详细分类
        /// </summary>
        /// <param name="actionId"></param>
        /// <returns>json</returns>
        public string Delete(string actionId)
        {
            int intId = Utils.StringToInt(actionId, 0);
            if (intId != 0)
            {
                Normal_ZTMapEntity normal_ztmap = new Normal_ZTMapEntity();
                normal_ztmap.Id = intId;
                IDataService<Normal_ZTMapEntity> Normal_ZTMapSv = ServiceBuilder.BuildNormal_ZTMapService();
                if (Normal_ZTMapSv.Delete(normal_ztmap)== "")
                {
                    returnJson = "{success:'true'}";
                }
                else
                    returnJson = "{success:'false'}";
            }
            else
            {
                returnJson = "{success:'false'}";
            }
            return returnJson;
        }
        /// <summary>
        /// 删除专题详细分类
        /// </summary>
        /// <param name="actionId"></param>
        /// <returns>json</returns>
        public string Delete2(string actionId)
        {
            string articleIdList = (string)Utils.GetQueryAndFormString("moduleIdList", MethodType.Get, 300, 0, DataType.Str);
            Common.RegExpression.NumAndBarRegExpression regex = new CMN.Common.RegExpression.NumAndBarRegExpression();
            if (regex.IsMatch(articleIdList))
            {

                IDataService<Normal_ZTMapEntity> Normal_ZTMapSv = ServiceBuilder.BuildNormal_ZTMapService();
                int intId = Utils.StringToInt(actionId, 0);
                Normal_ZTMapEntity normal_ztmap = Normal_ZTMapSv.Look(intId);
                IDataService<Module_ArticleEntity> articleSv = ServiceBuilder.BuildModule_ArticleService();
                string[] idList = articleIdList.Split('_');

                string[] articleTagList = null;
                articleTagList = normal_ztmap.IdList.Split(',');
                normal_ztmap.IdList = "";
                for (int i = 0; i < articleTagList.Length; i++)
                {
                    int articleId = Utils.StringToInt(articleTagList[i], 0);

                    bool isHas = false;
                    //是否已经包括
                    for (int j = 0; j < idList.Length; j++)
                    {
                        if (articleTagList[i] == idList[j])
                        {
                            isHas = true;
                            break;
                        }

                    }
                    if (!isHas)
                    {
                        if (string.IsNullOrEmpty(normal_ztmap.IdList))
                        {
                            normal_ztmap.IdList = articleTagList[i];
                        }
                        else
                            normal_ztmap.IdList = string.Concat(normal_ztmap.IdList, ",", articleTagList[i]);

                    }
                }
                Normal_ZTMapSv.Update(normal_ztmap);
                returnJson = "{success:'true'}";


            }
            else
            {
                returnJson = "{success:'false',message:'参数类型不正确'}";
            }
            return returnJson;
        }

        /// <summary>
        /// 批量删除专题详细分类
        /// </summary>
        /// <param name="idList">id列表 234_4_5</param>
        /// <returns>json</returns>
        public string DeleteList(string actionId)
        {
            Common.RegExpression.NumAndBarRegExpression regex = new CMN.Common.RegExpression.NumAndBarRegExpression();
            if (regex.IsMatch(actionId))
            {

                IDataService<Normal_ZTMapEntity> Normal_ZTMapSv = ServiceBuilder.BuildNormal_ZTMapService();
                if (Normal_ZTMapSv.DeleteList(actionId)== "")
                {
                    returnJson = "{success:'true'}";
                }
                else
                    returnJson = "{success:'false'}";

            }
            else
            {
                returnJson = "{success:'false',message:'参数类型不正确'}";
            }
            return returnJson;
        }

    }
}
